/**
 * Created by chen on 18/6/8.
 */

var promotion_barcodes = ['ITEM00001','ITEM00006'];//优惠促销barcode 买二送一

window.onload = function(){

  getSubTotal();
  var arr_cart_list = JSON.parse(localStorage.getItem("arr_cart_list"))||[];

  var tpl = document.getElementById('template_cart').innerHTML;
  var inner_tpl_string = template(tpl, {list:arr_cart_list});
  document.getElementById('cart_list_info').innerHTML = inner_tpl_string;
  getCartListNum();
  getTotal(arr_cart_list);

};
function getSubTotal(){
  var arr_cart_list = JSON.parse(localStorage.getItem("arr_cart_list"))||[];
  for(var i = 0; i < arr_cart_list.length; i ++)
  {
    var subtotal = arr_cart_list[i].count * arr_cart_list[i].price;

    var is_find_item = _.find(promotion_barcodes,function(promotion_barcode)
    {
      return promotion_barcode == arr_cart_list[i].barcode;
    });
    arr_cart_list[i].subtotal = subtotal + "元";
    if (is_find_item != undefined && arr_cart_list[i].count >= 3)
    {
      var price = (arr_cart_list[i].count - Math.floor(arr_cart_list[i].count/3)) * arr_cart_list[i].price;
      arr_cart_list[i].subtotal = price + "元（原价："+subtotal+"元）";
    }
  }
  localStorage.setItem("arr_cart_list",JSON.stringify(arr_cart_list));
}

function addIOnetemToCart(){
  var barcode_id = this.event.target.id;
  barcode_id = barcode_id.substr(3);

  var arr_cart_list = JSON.parse(localStorage.getItem("arr_cart_list"))||[];
  //console.log(arr_cart_list);
  var find_click_item = _.find(arr_cart_list, function(item){
    return item.barcode == barcode_id;
  });
  find_click_item.count ++;
  console.log(arr_cart_list);

  //计算小计
  var subtotal = find_click_item.count * find_click_item.price;

  var is_find_item = _.find(promotion_barcodes,function(promotion_barcode)
  {
    return promotion_barcode == barcode_id;
  });
  find_click_item.subtotal = subtotal + "元";
  if (is_find_item != undefined && find_click_item.count >= 3)
  {
    var price = (find_click_item.count - Math.floor(find_click_item.count/3)) * find_click_item.price;
    find_click_item.subtotal = price + "元（原价："+subtotal+"元）";
  }

  localStorage.setItem("arr_cart_list",JSON.stringify(arr_cart_list));
  getCartListNum();

  //修改加减号中间的数据
  $("#num"+barcode_id).text(find_click_item.count);
  $("#subtotal"+barcode_id).text(find_click_item.subtotal);
  //求总计
  getTotal(arr_cart_list);
}
function getTotal(arr_cart_list){
  var total = 0;
  for(var i= 0; i < arr_cart_list.length; i ++)
  {
    total += arr_cart_list[i].count * arr_cart_list[i].price;
  }
  $("#total").text(total + "元");
}

function subOneItemToCart()
{
  var barcode_id = this.event.target.id;
  barcode_id = barcode_id.substr(3);

  var arr_cart_list = JSON.parse(localStorage.getItem("arr_cart_list"))||[];
  var find_click_item = _.find(arr_cart_list, function(item){
    return item.barcode == barcode_id;
  });
  find_click_item.count --;
  console.log(arr_cart_list);
  if (find_click_item.count > 0)
  {
    //计算小计
    var subtotal = find_click_item.count * find_click_item.price;

    var is_find_item = _.find(promotion_barcodes,function(promotion_barcode)
    {
      return promotion_barcode == barcode_id;
    });
    find_click_item.subtotal = subtotal + "元";
    if (is_find_item != undefined && find_click_item.count >= 3)
    {
      var price = (find_click_item.count - Math.floor(find_click_item.count/3)) * find_click_item.price;
      find_click_item.subtotal = price + "元（原价："+subtotal+"元）";
    }

    localStorage.setItem("arr_cart_list",JSON.stringify(arr_cart_list));
    getCartListNum();

    //修改加减号中间的数据
    $("#num"+barcode_id).text(find_click_item.count);
    $("#subtotal"+barcode_id).text(find_click_item.subtotal);
    //求总计
    getTotal(arr_cart_list);
  }
  else
  {
    arr_cart_list.remove(find_click_item.barcode);
    localStorage.setItem("arr_cart_list",JSON.stringify(arr_cart_list));

    var tpl = document.getElementById('template_cart').innerHTML;
    var inner_tpl_string = template(tpl, {list:arr_cart_list});
    document.getElementById('cart_list_info').innerHTML = inner_tpl_string;
  }
}
Array.prototype.indexOf = function(val) {
  for (var i = 0; i < this.length; i++) {
    if (this[i].barcode == val) return i;
  }
  return -1;
};
Array.prototype.remove = function(val) {
  var index = this.indexOf(val);
  if (index > -1) {
    this.splice(index, 1);
  }
};

function pay()
{
  window.location.href = "pay.html";
}